<template>
	<view>
		<view class="header-top">

		</view>
		<view class="header-top acea-row row-between-wrapper row-middle">
			<view @click="back">
				<img src="../static/images/backLast.png" style='width: 14rpx; height: 24rpx;margin-top: 15rpx;' alt="">
			</view>
			<view class="header-top-tittle">我的发布-商家服务</view>
			<view @click="toPage('/pages/releases/merchantService/merchantService?categoryOn=0')">
				<img src="../static/images/add.png" style='width: 40rpx; height: 40rpx;margin-top: 15rpx;' alt="">
			</view>
		</view>
		<view class="acea-row row-middle nav-bg">
			<scroll-view scroll-x="true" style="white-space: nowrap; display: flex;" scroll-with-animation
				:scroll-left="tabLeft" show-scrollbar="true">
				<view class="item" :style='"width:"+isWidth+"px"' :data-index="index"
					:class="[index===categoryOn?'item-on':'']"
					v-for="(item,index) in categoryList" :key="index" :id="'id'+index"
					@click="categoryNavClick(item,index)">
					<view class="acea-row row-middle row-center img">
						<image :src="item.icon" mode=""></image>
					</view>
					<view class="name">
						{{item.name}}
					</view>
				</view>
			</scroll-view>
		</view>
		<view class="acea-row row-middle type-bg">
			<view class="type" :class="typeOn==index?'type-on':''" v-for="(item,index) in types" :key="index" @click="typeNavClick(item,index)">
				{{item.name}}
			</view>
		</view>
		
		
		<div class="acea-row" style="color: #999;text-align: center;justify-content: center;" v-if="!datalist|| datalist.length<1">暂无数据</div>
		<view v-for="(item,index) in datalist" :key="item.id" style="margin-bottom: 20rpx;">
			<serve :item="item" @os="(k,v)=>os(index,k,v)" @opear="opear" @updateItem="((k,v)=>$set(datalist[index],k,v))" />
		</view>
		<u-loading-icon :show="moreShow"></u-loading-icon>
		<!-- <u-loading-page :loading="loading"></u-loading-page> -->
	</view>
</template>

<script>
	import serve from "../components/serve.vue"
	import {
		getTypeApi
	} from "@/api/api.js";
	import {
		OnMerchantApi
	} from '@/api/service.js'
	import {
		toPage
	} from '@/filters/commFilter.js'
	export default {
		components: {
			serve
		},
		data() {
			return {
				showProvider: false,
				countKey:0,
				isWidth: 65,
				isLeft: 0,
				tabLeft: 0,
				types: [{
					name: '全部',
					value: ''
				},{
					name: '提供服务',
					value: 0
				}, {
					name: '找服务',
					value: 1
				}],
				typeOn: 0,
				categoryList: [],
				categoryOn: 0,
				serviceCate: '', //服务类别
				serviceTitle: '', //标题
				servicePoint: '', //服务特色
				servicePrice: '', //服务价格
				serviceAddress: '', //服务地址
				contactList: [1, 2],
				supplementText: '', //补充详情
				maxLength: 200,
				datalist: [],
				loading: true,
				page: 1,
				totalPage: 0,
				moreShow: false
			}
		},
		mounted() {
			this.getTypeList();
		},
		onReachBottom() {
			if(this.page >= this.totalPage) return
			this.loading = true;
			this.moreShow = true;
			this.page += 1;
			this.getList()
		},
		methods: {
			toPage,
			//获取列表
			async getList(){
				try{
					let { data } = await OnMerchantApi({typeId: this.categoryList[this.categoryOn].id, type: this.types[this.typeOn].value,page: this.page,limit: 10 });
					this.datalist = data.list;
					this.totalPage = data.totalPage;
					this.loading = false;
					this.moreShow = false;
				}catch(e){
					console.log(e)
					//TODO handle the exception
				}
			},
			os(index,k,v){
				console.log(index,k,v)
				this.datalist[index][k]=v
				console.log(this.datalist)
				// this.datalist = []
				// let d = this.datalist;
				// this.datalist = [];
				// this.$nextTick(()=>{
				// 	this.datalist= d;
				// })
				// this.countKey++;
			},
			opear(){
				this.page = 1;
				this.getList();
			},
			//服务类别
			async getTypeList() {
				try {
					let res = await getTypeApi({
						limit: 9999
					});
					console.log('iconList',res.data.list)
					this.categoryList = res.data.list;
					this.categoryList.unshift({
						id: '',name:'全部',icon:'/pages/releases/static/images/all.png'
					})
					this.getList()
				} catch (e) {
					//TODO handle the exception
				}
			},
			typeNavClick(item, index) {
				// let title = '发布-'+this.categoryList[this.categoryOn].name+item.name
				// uni.setNavigationBarTitle({
				// 	title:title
				// })
				this.typeOn = index
				this.loading = true
				this.getList()
			},
			// 导航栏点击
			categoryNavClick(item, index) {
				if (index == 5 || index == 6) {
					let title = '发布-商家服务-' + item.name
					uni.setNavigationBarTitle({
						title: title
					})
				} else {
					let title = '发布-商家服务'
					uni.setNavigationBarTitle({
						title: title
					})
				}

				if (this.categoryList.length > 6) {
					var tempIndex = index - 2;
					tempIndex = tempIndex <= 0 ? 0 : tempIndex;
					this.tabLeft = (index - 2) * this.isWidth //设置下划线位置
				}
				this.categoryOn = index //设置导航点击了哪一个
				this.isLeft = index * this.isWidth //设置下划线位置
				this.loading = true
				this.getList()
			},
			back(){
				uni.navigateBack()
			}
		}
	}
</script>

<style lang="scss">
	@import '/static/css/releaseForm.css';

	.nav-bg {
		background: #ffffff;
		padding: 18rpx 30rpx 36rpx;

		// overflow-x: scroll;
		// white-space: normal!important;
		.item~.item {
			margin-left: 36rpx;
		}

		.item.item-mr-l {
			margin-left: 24rpx;
		}

		// .item:nth-child(5)~.item{
		// 	margin-left: 24rpx;
		// }
		.item {
			display: inline-block;
			white-space: nowrap;

			.img {
				margin: 0 auto;
				width: 70rpx;
				height: 70rpx;
				border-radius: 50%;
				background: #ffffff;

				image {
					width: 70rpx;
					height: 70rpx;
				}
			}

			.name {
				// width: 108rpx;
				font-size: 26rpx;
				font-weight: 400;
				line-height: 40rpx;
				color: rgba(0, 0, 0, 1);
				text-align: center;
			}
		}

		.item-on {
			.img {
				background: rgba(247, 247, 247, 1);
			}

			.name {
				color: rgba(239, 39, 50, 1);
			}
		}
	}

	.type-bg {
		padding: 24rpx 30rpx;
		background: rgba(247, 248, 250, 1);

		.type~.type {
			margin-left: 18rpx;
		}

		.type {
			padding: 6rpx 24rpx;
			border-radius: 10rpx;
			border: 1px solid rgba(229, 229, 229, 1);
			font-size: 32rpx;
			font-weight: 400;
			color: rgba(128, 128, 128, 1);
		}

		.type-on {
			border: 1px solid rgba(238, 33, 45, 1);
			background: rgba(238, 33, 45, 1);
			color: #ffffff;

		}
	}

	.header-top {
		height: 88rpx;
		display: flex;
		align-items: center;
		background: rgba(238, 33, 45, 1);
		padding: 0rpx 30rpx;

		.header-top-tittle {
			font-size: 36rpx;
			color: rgba(255, 255, 255, 1);
		}
	}
</style>